package tests;

import java.lang.System;
import java.nio.ByteBuffer;

import global.AttrType;
import global.Convert;
import global.RID;
import global.SystemDefs;

import java.io.*;
import java.util.Arrays;
import java.util.List;

import diskmgr.PCounter;
import diskmgr.Stream;

import tripleheap.InvalidSlotNumberException;
import tripleheap.InvalidTripleSizeException;
import tripleheap.THFBufMgrException;
import tripleheap.THFDiskMgrException;
import tripleheap.THFException;
import tripleheap.Triple;
import tripleheap.TripleHeapfile;
import labelheap.LabelHeapFile;
import global.*;
import heap.Scan;
import heap.Tuple;

public class Query {
	
	public static void main(String[] args) throws InvalidSlotNumberException, InvalidTripleSizeException, THFException, THFDiskMgrException, THFBufMgrException, Exception {
		int i = 0;
		System.out.println("\n Query...\n");
		String a = args[0];
		int b = Integer.parseInt(args[1]);
		String c = args[2];
		String d = args[3];
		String e = args[4];
		Double f = Double.parseDouble(args[5]);
		int g = Integer.parseInt(args[6]);

		File file = new File(a);
		boolean exists = file.exists();
		if (exists) {
			SystemDefs.MINIBASE_RESTART_FLAG=false;
			SystemDefs sysdef = new SystemDefs(a, 0, 5500, "Clock");
		} else {
			System.out.println("DB does not exits");
		}
			//System.out.println("No of reads : "+PCounter.rcounter+"\nNo of writes : "+PCounter.wcounter);
			
			///home/kanchan/Desktop/phase2.txt 1 0 knows 0 0 100
			
		
		AttrType[] attrType = new AttrType[4];
		
		attrType[0] = new AttrType(AttrType.attrString);
		attrType[1] = new AttrType(AttrType.attrString);
		attrType[2] = new AttrType(AttrType.attrString);
		//attrType[3] = new AttrType(AttrType.attrDouble);
		attrType[3] = new AttrType(AttrType.attrReal);
		
		short[] strSize = new short[3];
		strSize[0] = 100;
		strSize[1] = 100;
		strSize[2] = 100;
			Stream s1=SystemDefs.JavabaseDB.openStream(a,b,c,d,e,f,g);
			
			Tuple tmpTuple = s1.get_next();
			while(true)
			{
				
				if(tmpTuple == null)
					break;
				else
				{
					tmpTuple.setHdr((short)4, attrType, strSize);
					String sub = tmpTuple.getStrFld(1);
					String pred = tmpTuple.getStrFld(2);
					String obj = tmpTuple.getStrFld(3);
					//double conf = tmpTuple.getDblFld(4);
					float conf = tmpTuple.getFloFld(4);
					System.out.println(sub + "\t\t"+ pred +"\t\t"+obj + "\t\t"+conf);
				}
				tmpTuple = s1.get_next();
			}
			
			s1.close();
			System.out.println("No of reads : "+PCounter.rcounter+"\nNo of writes : "+PCounter.wcounter);
			SystemDefs.JavabaseDB.closeDB();
			//"db1", 0, "0", "knows","0", 0, 10   :: /home/omkarm/Desktop/data.txt 0 0 knows 0 0 10   ::  dbname 2 0 knows 0 0 100
	}

}